﻿// ===============================================================================
// Copyright(c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// ===============================================================================

using System;
using XCI.Core;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;

namespace XCI.Sys.Forms
{
    public partial class FrmDeptDetails : BaseForm
    {
        public FrmDeptDetails(string deptId)
        {
            InitializeComponent();
            tabPane.RecordId = deptId;
        }

        public FrmDeptDetails(TreeListPlus tree)
        {
            InitializeComponent();
            tabPane.Tree = tree;
        }

        private async void FrmDeptDetails_Load(object sender, EventArgs e)
        {
            await tabPane.BindingModelAsync();
        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            DialogCancel();
        }

        private void TabPane_PageBinding(object sender, DetailsFormPaneBindingEventArgs e)
        {
            e.Task = async () =>
            {
                switch (e.Current.TabPageName)
                {
                    case nameof(tabBasic):
                        detailsSource.DataSource = await SysDeptService.Instance.SelectByIdAsync(e.Current.Id);
                        break;
                    case nameof(tabUserMember):
                        await owerUserGrid.LoadAsync();
                        break;
                    case nameof(tabHistoryLog):
                        await historyLogGrid1.LoadAsync(nameof(SysDept), e.Current.Id);
                        break;
                }
            };
        }

        private void OwerUserGrid_LoadData(object sender, LoadDataEventArgs e)
        {
            var filter = new SysUserFilter { DeptId = tabPane.Current.Id };
            e.Task = async () => new PagingList<SysUser>(await SysUserService.Instance.SelectListAsync(filter));
        }
    }
}